var PathManager = pc.createScript('PathManager');
PathManager.attributes.add('circlemat',{type:'asset'});
PathManager.attributes.add('boxmat',{type:'asset'});
PathManager.attributes.add('circle1mat',{type:'asset'});
PathManager.attributes.add('box1mat',{type:'asset'});


// initialize code called once per entity
PathManager.prototype.initialize = function() {
   
    var app = this.app; 
    var manager = app.root.findByName('Manager');
    this.gamemanager = manager.script.GameStateManager;
    this.gameparam = manager.script.GameParam;
    var player = app.root.findByName('player');
    this.playercontrol = player.script.PlayerControl;    
    this.CircleMat = app.assets.get(this.circlemat.id).resource;
    this.BoxMat = app.assets.get(this.boxmat.id).resource;
    this.Circle1Mat = app.assets.get(this.circle1mat.id).resource;
    this.Box1Mat = app.assets.get(this.box1mat.id).resource;
    
    this.blockdis = 0.5;
    this.mindis = 8;
    this.maxdis = 12;
    this.initcount = 3;
   
    this.count = 5;
    this.LY = 0;
    this.RY = 0;
    this.lindex = 0;
    this.rindex = 0;
    this.checklindex = 0;
    this.checkrindex = 0;
    this.CreateBlocks();
    this.InitPath();
};


PathManager.prototype.CreateBlocks = function(){
    var app = this.app;
    var prefab = app.root.findByName('prefab');
    this.lprefabs = [];
    this.rprefabs = [];
    this.lprefabs.push(prefab.script.BoxControl);
    var i=0,obj=null;
    for(i=1;i<this.count;i++)
    {
        obj = prefab.clone();
        app.root.addChild(obj);
        this.lprefabs.push(obj.script.BoxControl);
    }
    for(i=0;i<this.count;i++)
    {
        obj = prefab.clone();
        app.root.addChild(obj);
        this.rprefabs.push(obj.script.BoxControl);
    } 
};

PathManager.prototype.InitPath = function(){
    
    for(var i=0;i<this.count;i++)
    {
        if(i >= this.initcount)
        {
            this.lprefabs[i].Hide();
            this.rprefabs[i].Hide();
            continue;
        }
        this.SpanBox(true);
        this.SpanBox(false);
        
        
    }
    
    
};

PathManager.prototype.SpanBox = function(lr){
    
    var boxcontrol = null;
    var canhit = pc.math.random(0,1)>0.5;
    var leftorright = pc.math.random(0,1)>0.5;
    var offset = Math.floor(pc.math.random(this.mindis,this.maxdis))*this.blockdis;
    var x = 0;
    if(lr)
    {
        x =  leftorright?this.gameparam.playerpositions[0]:this.gameparam.playerpositions[1];
        boxcontrol = this.lprefabs[this.lindex];
        boxcontrol.body.model.model.meshInstances[0].material = canhit?this.BoxMat:this.CircleMat;
        this.LY += offset;
        boxcontrol.Init(x,this.LY,canhit);
        this.lindex = this.lindex>=this.count-1?0:this.lindex+1;
    }
    else
    {
        x =  leftorright?this.gameparam.playerpositions[2]:this.gameparam.playerpositions[3];
        boxcontrol = this.rprefabs[this.rindex];
        boxcontrol.body.model.model.meshInstances[0].material = canhit?this.Box1Mat:this.Circle1Mat;
        this.RY += offset;
        boxcontrol.Init(x,this.RY,canhit);
        this.rindex = this.rindex>=this.count-1?0:this.rindex+1;
    }
};

PathManager.prototype.Check = function(){
    var lboxcontrol = this.lprefabs[this.checklindex],
        rboxcontrol = this.rprefabs[this.checkrindex];
    var lstate = lboxcontrol.CheckHit(this.playercontrol.Lx,this.playercontrol.Ly),
        rstate = rboxcontrol.CheckHit(this.playercontrol.Rx,this.playercontrol.Ry);
    this.HandleCheck(lstate,true);
    this.HandleCheck(rstate,false);
    
};

PathManager.prototype.HandleCheck = function(state,lr){
    switch(state)
    {
        case -2:
            this.gamemanager.lose = true;
            this.playercontrol.PlaySound('lose2');
            break;
        case -1:
            this.gamemanager.lose = true;
            this.playercontrol.PlaySound('lose1');
            break;
        case 0:
            if(lr)
            {
                this.checklindex = this.checklindex>=this.count-1?0:this.checklindex+1;
            }
            else
            {
                this.checkrindex = this.checkrindex>=this.count-1?0:this.checkrindex+1;
            }
            this.SpanBox(lr);
            break;
        case 1:
            this.playercontrol.AddScore(lr);
            break;
        default:
            break;
    }
    
    
};

PathManager.prototype.Init = function(){
    this.mindis = 8;
    this.maxdis = 12;
    this.LY = 0;
    this.RY = 0;
    this.lindex = 0;
    this.rindex = 0;
    this.checklindex = 0;
    this.checkrindex = 0;
    this.InitPath();
};


